### For table output
library(memisc)
library(pander)
### For plotting marginal effects
if (!require(margins)) {
    library(devtools)
    install_github("leeper/prediction")
    install_github("leeper/margins")
}


capwords <- function(s, strict = FALSE) {
         cap <- function(s) paste(toupper(substring(s, 1, 1)),
                       {s <- substring(s, 2); if(strict) tolower(s) else s},
                                  sep = "", collapse = " " )
         sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s)))
}



dat <- read.csv("public_law_replication.csv")

### Model 1: Logistic regression of choice of male junior, conditional
### on male senior, two lawyer teams only

mod1 <- glm(maleJunior ~ maleSenior, 
	data = subset(dat, teamSize == 2),
	family = binomial)

### Export Table 2

table2 <- mtable('Model 1' = mod1)
pander(table2)

### Model 2: Logistic regression of choice of male junior, conditional
### on male senior, teams of any size
mod2 <- glm(maleJunior ~ maleSenior, 
	data = dat,
	family = binomial)

### Export Table 3
table3 <- mtable('Model 2' = mod2)
pander(table3)

### Model 3: Logistic regression, area composition taken into account
mod3 <- glm(maleJunior ~ maleSenior + propMale, 
	data = dat,
	family = binomial)

### Model 4: Logistic regression, chamber composition taken into accout
mod4 <- glm(maleJunior ~ maleSenior + PropMaleChambers.senior, 
	data = dat,
	family = binomial)

### Model 5: Logistic regression, interaction between chamber composition and size
mod5 <- glm(maleJunior ~ maleSenior + propMale +
                PropMaleChambers.senior * Size.senior, 
	data = dat,
	family = binomial)

### Output appendix Table 4
table4 <- mtable('Model 3'=mod3, 'Model 4'=mod4,'Model 5'=mod5)
pander(table4)

### Output appendix figure 3
png(file = "appendix_fig.png", width = 7 * 300, height = 4 * 300)
par(mar = c(7, 12, 4, 4))
cplot(mod5, dx = "PropMaleChambers.senior",
      x = "Size.senior",
      what = "effect",
      xlab = "Size of the senior lawyer's chambers",
      ylab = "Effect of a change in the % male of the senior lawyer's \nchambers on probability of a male junior\n",
      main = "As chambers get larger, their gender composition matters less",
      se.type = "shade",

      cex.lab = 3,
      cex.axis = 3,
      cex.main = 3,
      rug = FALSE)
dev.off()

